package utils;

import org.apache.commons.lang3.StringUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @ClassName DateUtil
 * @Deacription
 * @Author wushumin
 * @Date 2020-08-07 18:30
 * @Version 1.0
 **/
public class DateUtil {
    public final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * "yyyy-MM-dd HH:mm:ss"
     * @param dateTime
     * @return
     */
    public static Long transLateDateTime2Long(String dateTime) {
        if(StringUtils.isEmpty(dateTime)){
            return null;
        }
        if(dateTime.contains(".")){
            dateTime=dateTime.substring(0,dateTime.indexOf("."));
        }
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, dateTimeFormatter);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    /**
     * "yyyy-MM-dd"
     * @param date
     * @return
     */
    public static Long transLateDateStr2Long(String date){
        if(date==null){
            return null;
        }
        LocalDate localDate = LocalDate.parse(date, dateFormatter);
        LocalDateTime localDateTime = localDate.atTime(0, 0, 0, 0);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static Long transLateDate2Long(Date date){
        if(date==null){
            return null;
        }
        return date.getTime();
    }

    public static Long getDateLong(Long time) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        LocalDateTime localDate = localDateTime.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
        return localDate.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static String getCurrentDateStr(){
        return LocalDate.now().format(dateFormatter);
    }

    public boolean isCurrentDay(Long time){

    }


    public static void main(String[] args) {
        System.out.printf(DateUtil.getCurrentDateStr());
    }
}
